package com.lgmshare.k3.ui;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lgmshare.component.helper.NotificationHelper;
import com.lgmshare.component.logger.Logger;
import com.lgmshare.component.utils.ArithmeticUtils;
import com.lgmshare.component.utils.ContextUtils;
import com.lgmshare.component.utils.EncodeUtils;
import com.lgmshare.component.utils.EncryptUtils;
import com.lgmshare.component.utils.FileUtils;
import com.lgmshare.component.utils.SharedPreferencesUtils;
import com.lgmshare.component.utils.StatusBarUtils;
import com.lgmshare.component.utils.UIUtils;
import com.lgmshare.component.utils.ZUtils;
import com.lgmshare.component.widget.recyclerview.GridItemDecoration;
import com.lgmshare.component.widget.recyclerview.RecyclerViewAdapter;
import com.lgmshare.component.widget.recyclerview.RecyclerViewHolder;
import com.lgmshare.k3.K3Constants;
import com.lgmshare.k3.R;
import com.lgmshare.k3.manager.UserInfoViewModel;
import com.lgmshare.k3.model.UserInfo;
import com.lgmshare.k3.ui.base.BaseActivity;
import com.lgmshare.k3.ui.list.MessageListActivity;
import com.lgmshare.k3.ui.test.TestMediaHelperActivity;
import com.lgmshare.k3.ui.test.TestMediaPickerActivity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MainActivity extends BaseActivity {

    @BindView(R.id.iv_logo)
    ImageView ivLogo;
    @BindView(R.id.tv_appName)
    TextView tvAppName;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private UserInfoViewModel mUserInfoViewModel;

    private Observer<UserInfo> mUserInfoObserver = new Observer<UserInfo>() {
        @Override
        public void onChanged(UserInfo userInfo) {

        }
    };

    @Override
    protected int onBindLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void initLoad() {
        StatusBarUtils.setTranslucentForImageView(getActivity(), 0, null);
        StatusBarUtils.setLightMode(getActivity());
        mUserInfoViewModel = AppUIKit.getAppScopeViewModel(UserInfoViewModel.class);
        mUserInfoViewModel.userInfoLiveData().observeForever(mUserInfoObserver);
        tvAppName.setText(ContextUtils.getDeviceName());

        List<String> list = new ArrayList<>();
        list.add("ArithmeticUtils");
        list.add("ContextUtils");
        list.add("DatetimeUtils");
        list.add("EncodeUtils");
        list.add("EncryptUtils");
        list.add("FileUtils");
        list.add("ImageUtils");
        list.add("JSONUtils");
        list.add("NetworkUtils");
        list.add("PermissionCheckUtils");
        list.add("RegexUtils");
        list.add("SharedPreferencesUtils");
        list.add("StringUtils");
        list.add("HanZiToPinyinUtils");
        list.add("StatusBarUtils");
        list.add("UIUtils");
        list.add("ZUtils");
        list.add("MediaPicker");
        list.add("MediaHelper");
        list.add("NotificationHelper");

        ItemAdapter adapter = new ItemAdapter(getActivity());
        adapter.setOnItemClickListener(new RecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                clickItem(position);
            }
        });
        adapter.setList(list);
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        recyclerView.addItemDecoration(new GridItemDecoration(getActivity(), 3, 8, true));
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUserInfoViewModel.userInfoLiveData().removeObserver(mUserInfoObserver);
    }

    private void clickItem(int index) {
        switch (index) {
            case 0:
                clickArithmeticUtils();
                break;
            case 1:
                clickContextUtils();
                break;
            case 2:
                clickDatetimeUtils();
                break;
            case 3:
                clickEncodeUtils();
                break;
            case 4:
                clickEncryptUtils();
                break;
            case 5:
                clickFileUtils();
                break;
            case 6:
                clickImageUtils();
                break;
            case 7:
                clickJSONUtils();
                break;
            case 8:
                clickNetworkUtils();
                break;
            case 9:
                clickPermissionCheckUtils();
                break;
            case 10:
                clickRegexUtils();
                break;
            case 11:
                clickSharePreferencesUtils();
                break;
            case 12:
                clickStringUtils();
                break;
            case 13:
                clickHanZiToPinyinUtils();
                break;
            case 14:
                clickStatusBarUtils();
                break;
            case 15:
                clickUIUtils();
                break;
            case 16:
                clickZUtils();
                break;
            case 17:
                startActivity(new Intent(getActivity(), TestMediaPickerActivity.class));
                break;
            case 18:
                startActivity(new Intent(getActivity(), TestMediaHelperActivity.class));
                break;
            case 19:
                NotificationHelper.createNotificationChannel(getActivity().getApplicationContext());
                NotificationHelper.show(getActivity(), "tag", "通知标题信息", "虽然，它们的坐标不一样，但都是定时任务jar包的依赖，我果断删除了其中的一个，最后重新运行一下项目，问题终于搞定！", R.drawable.ic_launcher);
                break;
        }
    }

    private void clickArithmeticUtils() {
        Logger.d(TAG, ArithmeticUtils.add(12.42, 425.5d));
        Logger.d(TAG, ArithmeticUtils.add(12.0, 25.56));
        Logger.d(TAG, ArithmeticUtils.add(12.12, 25.56));
        Logger.d(TAG, ArithmeticUtils.div(12.12, 25.56));
        Logger.d(TAG, ArithmeticUtils.mul(12.12, 25.56));
        Logger.d(TAG, ArithmeticUtils.sub(112.12, 25.56));
        Logger.d(TAG, ArithmeticUtils.getPercentValue(112.1242522, 5));
        Logger.d(TAG, ArithmeticUtils.getPercentValue(112.1242522));
    }

    private void clickContextUtils() {
        Logger.d(TAG, ContextUtils.getDeviceName());
        Logger.d(TAG, ContextUtils.getPackageInfo(getActivity()).packageName);
        Logger.d(TAG, ContextUtils.getVersionCode(getActivity()));
        Logger.d(TAG, ContextUtils.getVersionName(getActivity()));
        checkRequestPermission(Manifest.permission.READ_PHONE_STATE, new PermissionCheckCallback() {
            @Override
            public void onPermissionsGranted(String[] permissions) {
                Logger.d(TAG, ContextUtils.getDeviceID(getActivity()));
            }

            @Override
            public void onPermissionDenied(String[] permissions) {

            }
        });
    }

    private void clickDatetimeUtils() {

    }

    private void clickEncodeUtils() {
        byte[] fssses = EncodeUtils.base64Encode("fsss");
    }

    private void clickEncryptUtils() {
        Logger.d(TAG, EncryptUtils.encryptMD5ToString("dddd"));
    }

    private void clickFileUtils() {
        Logger.d(TAG, "=========================================");
        Logger.d(TAG, FileUtils.getFileMimeTypeFromUrl("https://www.baidu.com/xxxxxxxxx/image1.jpg"));
        Logger.d(TAG, FileUtils.getFileMimeTypeFromUrl("/xxx/fweg/xgewg/image2.jpeg"));
        Logger.d(TAG, FileUtils.getFileMimeTypeFromUrl("doucment://sss/fex/wegw/image3.png"));
        Logger.d(TAG, FileUtils.getFileMimeTypeFromUrl("url://sffw/gwg/g/gasga/image4.webp"));
        Logger.d(TAG, FileUtils.getFileMimeTypeFromUrl("image5.svg"));
        Logger.d(TAG, FileUtils.getFileMimeTypeFromUrl("image6.gif"));
        Logger.d(TAG, FileUtils.getFileMimeTypeFromUrl("image7.bmp"));
        Logger.d(TAG, FileUtils.getFileMimeTypeFromUrl("audio1.mp3"));
        Logger.d(TAG, FileUtils.getFileMimeTypeFromUrl("audio2.wma"));
        Logger.d(TAG, FileUtils.getFileMimeTypeFromUrl("audio3.aac"));
        Logger.d(TAG, FileUtils.getFileMimeTypeFromUrl("audio4.ogg"));
        Logger.d(TAG, FileUtils.getFileMimeTypeFromUrl("audio5.m3u"));
        Logger.d(TAG, FileUtils.getFileMimeTypeFromUrl("audio6.ac3"));
        Logger.d(TAG, FileUtils.getFileMimeTypeFromUrl("video1.mp4"));
        Logger.d(TAG, FileUtils.getFileMimeTypeFromUrl("video2.avi"));
        Logger.d(TAG, FileUtils.getFileMimeTypeFromUrl("video3.mpeg"));
        Logger.d(TAG, FileUtils.getFileMimeTypeFromUrl("video4.mpg"));
        Logger.d(TAG, FileUtils.getFileMimeTypeFromUrl("9.txt"));
        Logger.d(TAG, FileUtils.getFileMimeTypeFromUrl("10.html"));
        Logger.d(TAG, FileUtils.getFileMimeTypeFromUrl("11.zip"));
        Logger.d(TAG, FileUtils.getFileMimeTypeFromUrl("12.csv"));
        Logger.d(TAG, FileUtils.getFileMimeTypeFromUrl("13.java"));
        Logger.d(TAG, FileUtils.getFileMimeTypeFromUrl("14.cc"));
        Logger.d(TAG, FileUtils.getFileMimeTypeFromUrl("15.doc"));
        Logger.d(TAG, FileUtils.getFileMimeTypeFromUrl("16.pdf"));
        Logger.d(TAG, FileUtils.getFileMimeTypeFromUrl("16.pptx"));
        Logger.d(TAG, FileUtils.getFileMimeTypeFromUrl("17.xls"));
        Logger.d(TAG, FileUtils.getFileMimeTypeFromUrl("18.docx"));
        Logger.d(TAG, "==========================================");
        String path = FileUtils.getExternalFilePath(getActivity(), "test", "test.os");
        Logger.d(TAG, path);
        File file = FileUtils.getFile(path);
        Logger.d(TAG, file.getPath());
        Uri uri = FileUtils.getUri(getActivity(), file);
        Logger.d(TAG, uri);
        Logger.d(TAG, uri.toString());
        Logger.d(TAG, Uri.fromFile(file));
        Logger.d(TAG, "=========================================");
        //String filePath = FileUtils.getPath(getActivity(), uri);
        //Logger.d(TAG, filePath);

        UIUtils.runOnAsyncThread(new Runnable() {
            @Override
            public void run() {
                String[] files = K3Constants.FILES;
                for (int i = 0; i < files.length; i++) {
                    Logger.d(TAG, FileUtils.getFileMimeTypeFromUrl(files[i]));
                }
            }
        });

        UIUtils.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Logger.d(TAG, "------------------Environment API-----------------------");
                Logger.d(TAG, Environment.getExternalStorageState());
                Logger.d(TAG, Environment.getDataDirectory().getPath());
                Logger.d(TAG, Environment.getRootDirectory().getPath());
                Logger.d(TAG, Environment.getDownloadCacheDirectory().getPath());
                Logger.d(TAG, Environment.getExternalStorageDirectory().getPath());
                Logger.d(TAG, "设备的外存是否是用内存模拟的: " + Environment.isExternalStorageEmulated());
                Logger.d(TAG, "设备的外存是否是可以拆卸的: " + Environment.isExternalStorageRemovable());
                Logger.d(TAG, "系统提醒铃声:" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_ALARMS).getPath());
                Logger.d(TAG, "相机拍摄照片和视频:" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath());
                Logger.d(TAG, "文档:" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getPath());
                Logger.d(TAG, "下载:" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath());
                Logger.d(TAG, "电影:" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getPath());
                Logger.d(TAG, "音乐:" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getPath());
                Logger.d(TAG, "系统通知铃声:" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_NOTIFICATIONS).getPath());
                Logger.d(TAG, "图片:" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath());
                Logger.d(TAG, "系统广播:" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PODCASTS).getPath());
                Logger.d(TAG, "系统铃声:" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES).getPath());
                Logger.d(TAG, getActivity().getFilesDir().getPath());
                Logger.d(TAG, getActivity().getCacheDir().getPath());
                Logger.d(TAG, getActivity().getExternalFilesDir(Environment.DIRECTORY_MOVIES).getPath());
                Logger.d(TAG, getActivity().getExternalCacheDir().getPath());

                Logger.d(TAG, FileUtils.getPublicPath("new", "test.txt"));
                Logger.d(TAG, FileUtils.getPublicPath(Environment.DIRECTORY_RINGTONES));
                Logger.d(TAG, "===============getInternalCachePath=======================");
                Logger.d(TAG, FileUtils.getInternalCachePath(getActivity()));
                Logger.d(TAG, FileUtils.getInternalCachePath(getActivity(), "new"));
                Logger.d(TAG, FileUtils.getInternalCachePath(getActivity(), "new", "test.txt"));
                Logger.d(TAG, "================getInternalFilePath======================");
                Logger.d(TAG, FileUtils.getInternalFilePath(getActivity()));
                Logger.d(TAG, FileUtils.getInternalFilePath(getActivity(), "new"));
                Logger.d(TAG, FileUtils.getInternalFilePath(getActivity(), "new", "test.txt"));
                Logger.d(TAG, "================getInternalDirPath======================");
                Logger.d(TAG, FileUtils.getInternalDirPath(getActivity(), "testdir"));
                Logger.d(TAG, FileUtils.getInternalDirPath(getActivity(), "testdir", "test.txt"));
                Logger.d(TAG, "================getExternalCachePath======================");
                Logger.d(TAG, FileUtils.getExternalCachePath(getActivity()));
                Logger.d(TAG, FileUtils.getExternalCachePath(getActivity(), "new"));
                Logger.d(TAG, FileUtils.getExternalCachePath(getActivity(), "new", "test.txt"));
                Logger.d(TAG, "================getExternalFilePath======================");
                Logger.d(TAG, FileUtils.getExternalFilePath(getActivity()));
                Logger.d(TAG, FileUtils.getExternalFilePath(getActivity(), "new"));
                Logger.d(TAG, FileUtils.getExternalFilePath(getActivity(), "new", "test.txt"));
                Logger.d(TAG, "=================getDiskCachePath=====================");
                Logger.d(TAG, FileUtils.getPackageCachePath(getActivity()));
                Logger.d(TAG, FileUtils.getPackageCachePath(getActivity(), "new"));
                Logger.d(TAG, FileUtils.getPackageCachePath(getActivity(), "new", "test.txt"));
                Logger.d(TAG, "=================getDiskFilePath=====================");
                Logger.d(TAG, FileUtils.getPackageFilePath(getActivity(), "new"));
                Logger.d(TAG, FileUtils.getPackageFilePath(getActivity(), "new", "test.txt"));
            }
        });
    }

    private void clickImageUtils() {
        //ImageUtils
    }

    private void clickJSONUtils() {
        //JSONUtils
    }

    private void clickNetworkUtils() {
        //NetworkUtils
    }

    private void clickPermissionCheckUtils() {
        //PermissionCheckUtils
    }

    private void clickRegexUtils() {
        //RegexUtils
    }

    private void clickSharePreferencesUtils() {
        Logger.d(TAG, "name:" + SharedPreferencesUtils.getString("name1"));
        Logger.d(TAG, "name:" + SharedPreferencesUtils.getString("name2"));
        Logger.d(TAG, "name:" + SharedPreferencesUtils.getString("name3"));
        Logger.d(TAG, "name:" + SharedPreferencesUtils.getString("name4"));
        SharedPreferencesUtils.putString("name1", "leo");
        SharedPreferencesUtils.putString("name2", "nova");
        SharedPreferencesUtils.putString("name3", "lara");
        SharedPreferencesUtils.putString("name4", "android");
        SharedPreferencesUtils.getSharedPreferences("");

        SharedPreferencesUtils.putString("key", "xxxxxxxxxxxx");
        SharedPreferencesUtils.putString("key1", "yyyyyyyyyyyy");
        SharedPreferencesUtils.putStringFromFile("test", "key1", "xxxxxxxxxxxx");
        Logger.d(TAG, SharedPreferencesUtils.getString("key"));
        Logger.d(TAG, SharedPreferencesUtils.getString("key1"));
        Logger.d(TAG, SharedPreferencesUtils.getStringFromFile("test", "key1"));
        Logger.d(TAG, SharedPreferencesUtils.getStringFromFile("test1", "key1"));
    }

    private void clickStringUtils() {
        //StringUtils
    }

    private void clickHanZiToPinyinUtils() {
        //HanZiToPinyinUtils
    }

    private void clickStatusBarUtils() {
        //StatusBarUtils
    }

    private void clickUIUtils() {
        Logger.d(TAG, UIUtils.getDisplayWidth());
        Logger.d(TAG, UIUtils.getDisplayHeight());
        Logger.d(TAG, UIUtils.getStatusBarHeight());
        Logger.d(TAG, UIUtils.getNavigationBarHeight());
        Logger.d(TAG, UIUtils.density);
        Logger.d(TAG, UIUtils.densityDpi);
        Logger.d(TAG, UIUtils.scaledDensity);
        Logger.d(TAG, UIUtils.ydpi);
        Logger.d(TAG, UIUtils.xdpi);
        UIUtils.copyTextToClip("xxxxxxxxxxxxxxxxx");
        Logger.d(TAG, UIUtils.getTextFromClip());
        Logger.d(TAG, UIUtils.getDimensionPixelSize(R.dimen.main_bottom_navigation_height));
        Logger.d(TAG, UIUtils.getDimensionPixelSize(R.dimen.ts_48));
        Logger.d(TAG, "px:" + UIUtils.spToPx(48));
        Logger.d(TAG, UIUtils.getString(R.string.app_name));

        startActivity(new Intent(getActivity(), MessageListActivity.class));
    }

    private void clickZUtils() {
        ZUtils.shareText(getActivity(), "分享到", "msgTitle", "msgText");
    }

    class ItemAdapter extends RecyclerViewAdapter<String> {

        public ItemAdapter(Context context) {
            super(context);
        }

        @Override
        protected int onBindItemViewResId() {
            return R.layout.adapter_text;
        }

        @Override
        protected void onBindItemViewData(RecyclerViewHolder holder, String item) {
            holder.setText(R.id.tv_title, item);
        }
    }
}
